package Messages;

import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.swing.JOptionPane;

import ocsf.server.ConnectionToClient;
import server.SQLConnection;
/**
 * This class implements the Add vase  to DB
 * @author Gabi Revah
 * @author Aviad Almog
 */
public class MessageAddVase {
	private ConnectionToClient client ;
	private String[] str; 
/**
 * Constructor of this class 
 * @param str
 * @param client
 */
	public MessageAddVase(String[] str, ConnectionToClient client) {
		this.client= client ;
		this.str= str; 	
	}
/**
 * This method add vase to DB 	
 * @throws SQLException
 * @throws IOException
 */
	public void AddVase() throws SQLException, IOException{
		SQLConnection sc = new SQLConnection();
		java.sql.PreparedStatement vase = sc.getConnection().prepareStatement("SELECT * FROM vases where name='"+str[1]+"' and color='"+str[2]+"';");
		ResultSet vsrs= vase.executeQuery();
		if(vsrs.next()){
			ArrayList<Object> bus=new ArrayList<Object>();
			bus.add("VaseExist");		
			client.sendToClient(bus);
		}
		else{
		java.sql.PreparedStatement vsins = sc.getConnection().prepareStatement("INSERT INTO vases (name,color,height,suppID,inventorystock,rentstock) VALUES(?,?,?,?,?,?);");		
			vsins.setString(1,str[1]);					
			vsins.setString(2,str[2]);
			vsins.setString(3,str[3]);
			vsins.setString(4,str[4]);
			vsins.setString(5,str[5]);
			vsins.setString(6,"0");
			vsins.executeUpdate();
			vsins.close();
			ArrayList<Object> bus=new ArrayList<Object>();
			bus.add("AddVase");		
			client.sendToClient(bus);
		}
	}

}
